package com.tuzhi.util;

import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.common.exception.BusinessException;

public class UserContext {
    private static final ThreadLocal<LoginUserBO> USER_INFO = new ThreadLocal<>();

    public static void setUserInfo(LoginUserBO loginUser) {
        USER_INFO.set(loginUser);
    }

    public static LoginUserBO getUserInfo() {
        return USER_INFO.get();
    }

    public static LoginUserBO getUserInfo(boolean isNullThrow) {
        LoginUserBO loginUserBO = USER_INFO.get();
        if (loginUserBO == null && isNullThrow) {
            throw new BusinessException("获取不到登录用户信息");
        }
        return loginUserBO;
    }

    public static void clear() {
        USER_INFO.remove();
    }
}
